package san.java.day13;

import java.io.*;

public class BufferCharStream_02 {

    public static void main(String[] args) {
        try {
            testBufferReader();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            testBufferedWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            copyFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testBufferReader() throws IOException {
        FileReader fr = new FileReader("data/a2.txt");

        BufferedReader br = new BufferedReader(fr);

        char[] c = new char[256];

        int len = 0;

        while ((len = br.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }

        br.close();
        fr.close();
    }

    /**
     * 缓冲字符输出流
     */
    public static void testBufferedWriter() throws IOException{
        FileWriter fw = new FileWriter("data/a2.txt");

        BufferedWriter bw = new BufferedWriter(fw);

        char[] c = new char[256];

        int len = 0;

        String s = "hello world!";
        bw.write(s);
        bw.close();
        fw.close();
    }

    public static void copyFile() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("data/a1.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/a5.txt"));

        char[] c = new char[4096];

        int len = 0;

        while ((len = br.read(c)) != -1) {
            bw.write(c, 0, len);
        }

        bw.flush();
        bw.close();
        br.close();

    }
}
